#ifndef _common_driver_H_
#define _common_driver_H_

/*起点:支持功能使能区域********************************************************/
#define dCommonFunctionEnable_Base//基础功能
//#define dCommonFunctionEnable_PID//PID功能
#define dCommonFunctionEnable_LinearFitting//线性拟合功能
#define dCommonFunctionEnable_STM32_Base//stm32基础功能:串口打印func重映射、GPIO操作
//#define dCommonFunctionEnable_STM32_TIM//stm32定时器功能
#define dCommonFunctionEnable_STM32_FreeRTOS//stm32操作系统功能


/*终点:支持功能使能区域********************************************************/

/*起点:头文件区域********************************************************/
#include "stdint.h"
#include "stdbool.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "limits.h"//提供了有关整数类型（char、short、int、long 和 long long 等）和其他数据类型的最大值和最小值的信息,例如：USHRT_MAX
/*终点:头文件区域********************************************************/

/*起点:宏定义区域********************************************************/
#define dCommon_PI (3.141592653)
#define dCommon_Bool(x) ((x)?true:false)//逻辑值:真-1,假-0
#define dCommon_Negation(x) ((x)?false:true)//逻辑取反
#define dCommon_Limit_Max(x,max) (((x) > (max))?(max):(x)) //限制x的范围最大值
#define dCommon_Limit_Min(x,min) (((x) < (min))?(min):(x)) //限制x的范围最小值
#define dCommon_Limit_Range(x,min,max) ((x) > (max))?(max):(((x) < (min))?(min):(x)) //限制x的范围为[min,max]
#define dCommon_Range_Check(x,min,max) ((((min) > (x)) || ((x) > (max)))?true:false)  //x超过范围[max,min]为真，否则为假

#define dCommon_Name_Bool(x) ((x)?"true":"false")//获取逻辑字符串

#define dLOG(...) printf(__VA_ARGS__)
#define dLOG_FUNC_LINE()  printf("%s line:%d\r\n",__func__,__LINE__)

/*终点:宏定义区域********************************************************/

/*起点:类型定义区域********************************************************/
#if 0
typedef  uint8_t    u8;
typedef  uint16_t   u16;
typedef  uint32_t   u32;
typedef  uint64_t   u64;
typedef  int8_t     s8;
typedef  int16_t    s16;
typedef  int32_t    s32;
typedef  int64_t    s64;
#endif

typedef struct
{
	unsigned char byte1;
	unsigned char byte2;
}Common_Byte2_MemoryParameterStructDef;
typedef struct
{
	unsigned char byte1;
	unsigned char byte2;
	unsigned char byte3;
	unsigned char byte4;
}Common_Byte4_MemoryParameterStructDef;
typedef struct
{
	unsigned char byte1;
	unsigned char byte2;
	unsigned char byte3;
	unsigned char byte4;
	unsigned char byte5;
	unsigned char byte6;
	unsigned char byte7;
	unsigned char byte8;
}Common_Byte8_MemoryParameterStructDef;

typedef union
{
	int16_t value;//short int
	Common_Byte2_MemoryParameterStructDef memory;
}Common_Convert_int16_Parameter_UnionDef;
typedef union
{
	uint16_t value;//unsigned short int
	Common_Byte2_MemoryParameterStructDef memory;
}Common_Convert_uint16_Parameter_UnionDef;

typedef union
{
	int32_t value;//long int
	Common_Byte4_MemoryParameterStructDef memory;
}Common_Convert_int32_Parameter_UnionDef;
typedef union
{
	uint32_t value;//unsigned long int
	Common_Byte4_MemoryParameterStructDef memory;
}Common_Convert_uint32_Parameter_UnionDef;

typedef union
{
	int64_t value;//long long int
	Common_Byte8_MemoryParameterStructDef memory;
}Common_Convert_int64_Parameter_UnionDef;
typedef union
{
	uint64_t value;//unsigned long long int
	Common_Byte8_MemoryParameterStructDef memory;
}Common_Convert_uint64_Parameter_UnionDef;

typedef union
{
	float value;
	Common_Byte4_MemoryParameterStructDef memory;
}Common_Convert_float_Parameter_UnionDef;

typedef union
{
	double value;
	Common_Byte8_MemoryParameterStructDef memory;
}Common_Convert_double_Parameter_UnionDef;

typedef struct
{
    uint16_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    uint8_t millisecond;
}Common_Time24Format_StructDef;//日期-时间结构体

/*终点:类型定义区域********************************************************/

/*起点:函数声明区域********************************************************/
#ifdef dCommonFunctionEnable_Base
void Common_LinearFitCoefficient(double *pA,double *pB,double x[],double y[],uint32_t dataSize);
uint32_t Common_DichotomyFindPos(float target,float *pdata,uint32_t len);
void Common_Generate_SineWave(uint8_t bits,float VoltageMax,uint16_t NPoints,float amplitude,float offset,uint16_t *SineWaveTable);
int32_t Common_Printf_HEX(uint8_t *message,uint8_t *pbuf, uint32_t len,uint8_t separator,bool prefix);
int32_t Common_Printf_Float(uint8_t *message,float *pbuf, uint32_t len,uint8_t separator,int8_t bits);
int32_t Common_Printf_Integer(uint8_t *message,void *pbuf, uint32_t len,uint8_t separator,int8_t type);
#endif

#ifdef dCommonFunctionEnable_Base
typedef struct
{
  float target;//目标值
  float actual;//当前输出值
  float err;//本次偏差值
  float err_last;//上一次偏差值
  float err_next;//上上次的偏差值
  float integral;//累计误差
  float Kp;
  float Ki;
  float Kd;
}Common_PID_Parameter_StructDef;//PID参数结构体
float Common_PID_realize_increment(Common_PID_Parameter_StructDef *pid,float actual_val,int64_t Min,int64_t Max);
float Common_PID_realize_location(Common_PID_Parameter_StructDef *pid,float actual_val,int64_t Min,int64_t Max);
#endif

#ifdef dCommonFunctionEnable_LinearFitting
//校准列表格式：
//设置值 实际值
//数据0 数据0
//... ...
//数据n-1 数据n-1
typedef struct//设置值=实际值*a + b
{
    uint32_t length;//创建点数个数,此参数创建变量时初始化为0
    uint32_t number;//校准数据实际点数,0时无校准数据,此参数创建变量时初始化为0
    float *real;//校准数据中的实际值
    float *a;//校准系数a
    float *b;//校准系数b
}Common_LinearFitting_Parameter_StructDef;//线性拟合参数
int32_t Common_LinearFitting_Init(Common_LinearFitting_Parameter_StructDef *p,uint32_t number);
void Common_LinearFitting_Deinit(Common_LinearFitting_Parameter_StructDef *p);
void Common_LinearFitting_Clear(Common_LinearFitting_Parameter_StructDef *p);
float Common_LinearFitting_Calculate(Common_LinearFitting_Parameter_StructDef p,float real);
#endif

#ifdef dCommonFunctionEnable_STM32_Base
#include "main.h"
#pragma diag_suppress 177 //忽略编译时函数定义但是没有引用的警告

#define dGPIO_Mode_IN(gpio,pin)             {gpio->MODER &= ~(3<<(pin*2));gpio->MODER |= 0<<(pin*2);}//配置引脚为输入模式
#define dGPIO_Mode_OUT(gpio,pin)            {gpio->MODER &= ~(3<<(pin*2));gpio->MODER |= 1<<(pin*2);}//配置引脚为输出模式
#define dGPIO_Pin_High(GPIOx,Pin)           GPIOx->BSRR = Pin  //引脚置1
#define dGPIO_Pin_Low(GPIOx,Pin)            GPIOx->BSRR = ((uint32_t)Pin << 16u) //引脚置0
#define dGPIO_Pin_Write(GPIOx,Pin,x)        GPIOx->BSRR = ((uint32_t)Pin << ((x)?0u:16u))//引脚电平设置
#define dGPIO_Pin_Read(GPIOx,Pin)           (GPIOx->IDR & Pin)?1:0 //获取引脚状态
#define dGPIO_Pin_Toggle(GPIOx,Pin)         HAL_GPIO_TogglePin(GPIOx,Pin)//引脚电平翻转

void Common_USB_Disconnet(void);
void Common_SystemReset(void);
void Common_SystemReset_Monitor(bool promptly);
#endif

#ifdef dCommonFunctionEnable_STM32_TIM
#include "tim.h"
int32_t Common_PWM_Channe_Set(TIM_HandleTypeDef *tim,uint32_t Channel,uint32_t Pulse,uint32_t Min,uint32_t Max);
int32_t Common_TIM_Configurate(TIM_HandleTypeDef *tim,uint16_t Prescaler,uint16_t Period);
int32_t Common_TIM_Frequency_Calculate(float frequency,uint32_t clock_frequency,uint32_t *prescaler,uint32_t *period);
int32_t Common_TIM_Frequency_Set(TIM_HandleTypeDef *tim,float frequency,uint32_t clock_frequency);
#endif

#ifdef dCommonFunctionEnable_STM32_FreeRTOS
void Common_FreeRTOS_TaskStack_Monitor(void *TaskHandle,char *TaskName,uint32_t size);
void Common_FreeRTOS_CPU_Usage_Display(void);
#endif
/*终点:函数声明区域********************************************************/


#endif
